﻿using HeSeHus.Import.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace HeSeHus.Import.Model
{
    public static class XMLParser
    {
        public static Dictionary<IProduct, List<IProductDetail>> ParseProduct(string sti)
        {
            XmlSerializer ser = new XmlSerializer(typeof(ProductFeed));

            ProductFeed parsing = ser.Deserialize(new FileStream(sti, FileMode.Open)) as ProductFeed;
            
            ProductCollection productColl = new ProductCollection();
           
            List<IProductDetail> productList = new List<IProductDetail>();
            IProductDetail pd;
            IProduct product;

            for (int i = 0; i < parsing.Products.Length; i++)
            {
                product = new Product(parsing.Products[i].ProductNumber, parsing.Products[i].BrandCode, parsing.Products[i].AvailabilityLevel, parsing.Products[i].Category, parsing.Products[i].ProductStatus.ToString(), parsing.Products[i].LaunchDate);
                for (int j = 0; j < parsing.Products[i].Descriptions.Length; j++)
                {
                    productList = new List<IProductDetail>();
                    for (int h = 0; h < parsing.Products[i].Descriptions.Length; h++)
                    {
                    
                        pd = new ProductDetail(parsing.Products[i].Descriptions[j].Language, parsing.Products[i].Descriptions[j].Description, parsing.Products[i].Descriptions[j].ReplacementNotes);

                        productList.Add((IProductDetail)pd);
                        j++;
                   }
                        productColl.Add(product,productList);
                }
                
           }
            Dictionary<IProduct,List<IProductDetail>> returDic = new Dictionary<IProduct,List<IProductDetail>>();
            
            
            foreach (var item in productColl)
            {
               
                returDic.Add((IProduct)item.Key, item.Value);
            }

            return returDic;
        }


        public static BrandCollection ParseBrand(string sti)
        {
            XmlSerializer ser = new XmlSerializer(typeof(BrandFeed));

            BrandFeed parsing = ser.Deserialize(new FileStream(sti, FileMode.Open)) as BrandFeed;

            BrandCollection brandColl = new BrandCollection();
            foreach (var item in parsing.Brands)
            {
                brandColl.Add(new Brand(item.BrandCode, item.BrandName));
            }
            return brandColl;
        }

        public static CategoryCollection ParseCategory(string sti)
        {
            XmlSerializer ser = new XmlSerializer(typeof(CategoryFeed));

            CategoryFeed parsing = ser.Deserialize(new FileStream(sti, FileMode.Open)) as CategoryFeed;

            CategoryCollection categoryColl = new CategoryCollection();
            foreach (var item in parsing.Categories)
            {
                categoryColl.Add(new Category(item.Level, int.Parse(item.Id), item.Name, item.Description, item.BrandCode));
            }
            
            
            return categoryColl;
        }
    }
}


